<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button
          type="primary"
          v-if="hasPermission(['sysOrg:add'])"
          @click="handleCreate"
        >
          {{ t('新增组织') }}
        </a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: '增加下级',
              auth: 'sysOrg:edit',
              onClick: handleAddSub.bind(null, record),
            },
            {
              label: '编辑',
              auth: 'sysOrg:edit',
              onClick: handleEdit.bind(null, record),
            },
            {
              label: '删除',
              auth: 'sysOrg:delete',
              color: 'error',
              popConfirm: {
                title: t('是否确认删除'),
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <DeptModal @register="registerModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
import { usePermission } from '@/hooks/web/usePermission';
import { BasicTable, useTable, TableAction } from '@/components/Table';
import { getOrgList } from '@/api/sys/system';
import { del } from '@/api/sys/restManager';
import { useMessage } from '@/hooks/web/useMessage';
import { useModal } from '@/components/Modal';
import DeptModal from './DeptModal.vue';

import { columns, searchFormSchema } from './dept.data';
import { useI18n } from '@/hooks/web/useI18n';

export default defineComponent({
  name: 'Depart',
  components: { BasicTable, DeptModal, TableAction },
  setup() {
    const { t } = useI18n();
    const { createMessage } = useMessage();
    const { hasPermission } = usePermission();
    const [registerModal, { openModal }] = useModal();
    const [registerTable, { reload }] = useTable({
      title: t('组织列表'),
      api: getOrgList,
      columns,
      formConfig: {
        labelWidth: 120,
        schemas: searchFormSchema,
      },
      pagination: false,
      striped: false,
      useSearchForm: true,
      bordered: true,
      showIndexColumn: false,
      canColDrag: true,
      beforeFetch: (params) => {
        params.hasDepart = true;
        return params;
      },
      actionColumn: {
        auth: ['sysOrg:add', 'sysOrg:edit', 'sysOrg:delete'],
        width: 200,
        title: t('操作'),
        dataIndex: 'action',
        slots: { customRender: 'action' },
        fixed: undefined,
      },
    });

    function handleCreate() {
      openModal(true, {
        isUpdate: false,
      });
    }

    function handleAddSub(record: Recordable) {
      openModal(true, {
        isUpdate: false,
        parent: record,
      });
    }

    function handleEdit(record: Recordable) {
      openModal(true, {
        record,
        isUpdate: true,
      });
    }

    function handleDelete(record: Recordable) {
      console.log(record);
      del('sysOrg', [record.id]).then(() => {
        createMessage.success(t('删除成功'));
        reload();
      });
    }

    function handleSuccess() {
      reload();
    }

    return {
      t,
      registerTable,
      registerModal,
      handleCreate,
      handleAddSub,
      handleEdit,
      handleDelete,
      handleSuccess,
      hasPermission,
    };
  },
});
</script>
